package com.example.askto;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Random;

public class RobotUtil {

    private static final Robot robot;

    /**
     * 缩放比例
     */
    public static double scaling = 1.5;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 回车
     */
    public static void enter() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ENTER);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

    /**
     * 回车
     */
    public static void esc() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ESCAPE);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ESCAPE);
    }

    /**
     * f8
     */
    public static void F8() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_F8);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_F8);
    }

    /**
     * alt+tab
     */
    public static void alt_tab() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ALT);
        sleep100_200();
        robot.keyPress(KeyEvent.VK_TAB);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_TAB);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ALT);
        System.out.println("切换一次");
    }

    /**
     * alt+esc
     */
    public static void alt_esc() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ALT);
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ESCAPE);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ESCAPE);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ALT);
        System.out.println("切换一次");
    }

    /**
     * ctrl+tab
     */
    public static void ctrl_tab() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_CONTROL); // ctrl
        sleep100_200();
        robot.keyPress(KeyEvent.VK_TAB);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_TAB);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_CONTROL); // ctrl
    }

    /**
     * alt+t 队伍
     */
    public static void alt_t() {
        System.out.println("alt_t");
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ALT);
        sleep100_200();
        robot.keyPress(KeyEvent.VK_T);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_T);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ALT);
    }

    /**
     * alt+w npc列表
     */
    public static void alt_w() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ALT);
        sleep100_200();
        robot.keyPress(KeyEvent.VK_W);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_W);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ALT);
    }

    /**
     * alt+f 好友
     */
    public static void alt_f() {
        sleep100_200();
        robot.keyPress(KeyEvent.VK_ALT);
        sleep100_200();
        robot.keyPress(KeyEvent.VK_F);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_F);
        sleep100_200();
        robot.keyRelease(KeyEvent.VK_ALT);
    }

    /**
     * 点击鼠标左键
     */
    public static void mouseLeft() {
        sleep100_200();
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        sleep100_200();
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    }

    /**
     * 鼠标定位切换。为了防止鼠标偏移，先切换到其他页面，再切换回来
     */
    public static void mouseMoveQh(int x, int y) {
        alt_esc();
        // 移动到选择任务的选项上
        mouseMove(x, y);
        //再切换回来
        alt_esc();
        System.out.println("切换完毕");
    }

    /**
     * 鼠标定位
     */
    public static void mouseMove(int x, int y) {
        robot.mouseMove(-1, -1);
        robot.mouseMove((int) (x / scaling), (int) (y / scaling));
    }


    /**
     * 睡眠100-200毫秒
     */
    public static void sleep100_200() {
        int intNum = getRandomNumInTwoIntNum(180, 182);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 睡眠100-200毫秒
     */
    public static void sleep500() {
        int intNum = getRandomNumInTwoIntNum(500, 600);
        try {
            Thread.sleep(intNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 睡眠100-200毫秒
     */
    public static void sleep800() {
        int intNum = getRandomNumInTwoIntNum(800, 850);
        try {
            Thread.sleep(intNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取随机数
     */
    public static int getRandomNumInTwoIntNum(int x, int y) {
        Random random = new Random();
        int cha = Math.abs(x - y);

        int randomCha = random.nextInt(cha) + 1;
        if (randomCha >= cha) {
            randomCha = cha - 1;
        }
        if (x > y) {
            return randomCha + y;
        }
        if (x < y) {
            return randomCha + x;
        }
        return x;
    }

    /**
     * 睡眠1000-2000毫秒
     */
    public static void sleepS(int s) {
        s = s * 1000;
        int intNum = getRandomNumInTwoIntNum(1000, 2000);
        s = s + intNum;
        try {
            Thread.sleep(s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 截取进入战斗时头像部位
     */
    public static BufferedImage screenShotTx() {
        Rectangle screenRect = new Rectangle(169, 124, 77, 77);

        return robot.createScreenCapture(screenRect);
    }
}
